import React, { Fragment, useState, forwardRef, useImperativeHandle } from "react";

const Child = forwardRef((_, ref) => {
    const [num, setNum] = useState(0);
    const increment = () => {
        setNum(num => num += 1)
    }
    /**
     * 将函数式组件中的值和改变值的方法以及私有方法都暴露给父组件
     */
    useImperativeHandle(ref, () => ({ setNum, increment }), [])
    return (
        <Fragment>
            <div className='alert alert-danger'>
                当前值为：{num}
            </div>
            <button onClick={increment} className='btn btn-primary'>增加</button>
        </Fragment>
    )
})

export default Child